package com.sky.exception;

public class SysException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private int errorCode;
    private String subCode;
    private String errorMsg;
    private Throwable cause;

    public SysException(int errorCode) {
        super(errorCode+"");
        this.errorCode = errorCode;
        this.subCode = "0";
        this.errorMsg = "undefined";
    }

    public SysException(int errorCode, String errorMsg) {
        super(errorCode+"");
        this.errorCode = errorCode;
        this.subCode = "0";
        this.errorMsg = errorMsg;
    }
    public SysException(ResultApi api) {
        super(api.code()+"");
        this.errorCode = api.code();
        this.subCode = "0";
        this.errorMsg = api.msg();
    }
    public SysException(ResultApi api, String errorMsg) {
        super(api.code()+"");
        this.errorCode = api.code();
        this.subCode = "0";
        this.errorMsg = errorMsg;
    }
    public SysException(int errorCode, String subCode, String errorMsg) {
        super(errorCode+"");
        this.errorCode = errorCode;
        this.subCode = subCode;
        this.errorMsg = errorMsg;
    }

    public SysException(int errorCode, Throwable cause) {
        super(errorCode+"", cause);
        this.errorCode = errorCode;
        this.subCode = "0";
        this.errorMsg = "undefined";
        this.cause = cause;
    }

    public SysException(int errorCode, String msg, Throwable cause) {
        super(errorCode+"", cause);
        this.errorCode = errorCode;
        this.subCode = "0";
        this.errorMsg = msg;
        this.cause = cause;
    }

    public SysException(int errorCode, String subCode, String msg, Throwable cause) {
        super(errorCode+"", cause);
        this.errorCode = errorCode;
        this.subCode = subCode;
        this.errorMsg = msg;
        this.cause = cause;
    }

    public String getInfo() {
        StringBuffer info = new StringBuffer();
        info.append(errorCode);
        if (!"0".equals(subCode)) {
            info.append("-" + subCode);
        }
        info.append(":");
        info.append(getErrorMsg());
        return info.toString();
    }

    public int getErrorCode() {
        return this.errorCode;
    }

    public String getSubCode() {
        return subCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public Throwable getCause() {
        return cause;
    }

}
